package com.xiaomai.center.controller;


import com.xiaomai.center.entity.RegisterVo;
import com.xiaomai.center.entity.UcenterMember;
import com.xiaomai.center.service.UcenterMemberService;
import com.xiaomai.commonutil.JwtUtils;
import com.xiaomai.commonutil.R;
import com.xiaomai.commonutil.ordervo.UcenterOrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-09-18
 */
@RestController
@RequestMapping("/educenter/member")

public class UcenterMemberController {

    @Autowired
   private UcenterMemberService memberService;

    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member){
          String token = memberService.login(member);
        return R.ok().data("token",token);
    }
    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
         memberService.register(registerVo);
        return R.ok();
    }

    //解析地址栏token
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo", member);
    }

    //根据用户Id查询用户信息并返回
    @PostMapping("getUserInfo/{Id}")
    public UcenterOrderVo getUserInfo(@PathVariable String id){
        UcenterMember byId = memberService.getById(id);
        UcenterOrderVo ucenterOrderVo = new UcenterOrderVo();
        BeanUtils.copyProperties(byId,ucenterOrderVo);
        return ucenterOrderVo;
    }
//        @PostMapping("getUserInfo/{id}")
//        public UcenterOrderVo getUserInfo(@PathVariable String id) {
//            UcenterMember member = memberService.getById(id);
//            //把member对象里面值复制给UcenterMemberOrder对象
//            UcenterOrderVo ucenterMemberOrder = new UcenterOrderVo();
//            BeanUtils.copyProperties(member, ucenterMemberOrder);
//            return ucenterMemberOrder;
//        }
    @GetMapping("countregister/{day}")
    public R countregister(@PathVariable String day){
       Integer count = memberService.countRegisterByDay(day);

        return R.ok().data("countRegister", count);
    }
}

